package com.wman.learn

import kotlin.reflect.KProperty

/**
 * @ProjectName: wman
 * @Package: com.wman.learn
 * @ClassName: test
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2020/7/13 16:30
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/7/13 16:30
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

fun main() {
    //类委托
    var b = ImplBase(10)
    Drived(b).print()

    //委托属性
    var user1 = User1()
    user1.userName = "user1"
    println(user1.userName)
    var user2 = User2()
    user2.userName = "user2"
    println(user2.userName)
}

interface Base {
    fun print()
}

class ImplBase(val i: Int) : Base {
    override fun print() {
        println("impBase $i")
    }
}

class Drived(b: Base) : Base by b

class User1 {
    var userName: String by Delegate()
}

class User2 {
    var userName: String by Delegate()
}

class Delegate {
    var userName = ""

    operator fun getValue(user2: Any, property: KProperty<*>): String {
        return userName
    }

    operator fun setValue(user2: Any, property: KProperty<*>, s: String) {
        userName = s
    }
}